home *** CD-ROM | disk | FTP | other *** search
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- (c) TechInsite Pty. Ltd.
- PO Box 429, Abbotsford, Melbourne. 3067 Australia
- Phone: +61 3 9419 6456
- Fax: +61 3 9419 1682
- Web: www.techinsite.com.au
- EMail: peter_hinrichsen@techinsite.com.au
-
- Created: October 1999
-
- Notes: Our 'Subject' class.
- We are modeling a portfolio of shares, so the main
- object (TPortfolio) is basically a TList of
- TStockTrans objects, which contain data about a
- transaction we made. Data in TStockTrans includes
- the share's name, company name, quantity and price.
-
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
- unit Subject_Portfolio;
-
- interface
- uses
- Classes
- ,FObserver_Abstract
- ;
-
- type
-
- // TPortfolio, a holder of TStockTrans objects, with an
- // additional method to calculate the total
- // portfolio value.
- //--------------------------------------------------------
- TPortfolio = class( TSubjectAbstract )
- private
- // A TList to hold the TStockTrans objects
- FStocks : TList ;
- public
- constructor create ;
- destructor destroy ; override ;
- property stocks : TList read FStocks ;
- // Load some test data into the object
- procedure LoadData ;
- // Calculate the total value of the portfolio
- function Total : real ;
- end;
-
- // A holder for data about a share:
- // StockCode, company name, quantity and price
- //--------------------------------------------------------
- TStockTrans = class( TObject )
- private
- FiQty: integer;
- FsStockCode: string;
- FsStockName: string;
- FrPrice: real;
- function GetValue: real;
- public
- constructor createExt( const pStockCode : string ;
- const pStockName : string ;
- const pQty : integer ;
- const pPrice : real ) ;
-
- property StockCode : string read FsStockCode write FsStockCode ;
- property StockName : string read FsStockName write FsStockName ;
- property Price : real read FrPrice write FrPrice ;
- property Qty : integer read FiQty write FiQty ;
- property Value : real read GetValue ;
- end ;
-
- // A function pointing to our single instance of the
- // TPortfolio class
- function gPortfolio : TPortfolio ;
-
- implementation
- uses
- SysUtils
- ;
-
- // Our single instance of the TPortfolio class
- var
- uPortfolio : TPortfolio ;
-
- // A 'poor man's singleton' (See the FactoryPattern article)
- //----------------------------------------------------------
- function gPortfolio : TPortfolio ;
- begin
- if uPortfolio = nil then begin
- uPortfolio := TPortfolio.Create;
- uPortfolio.LoadData ;
- end ;
- result := uPortfolio ;
- end ;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TPortfolio
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- constructor TPortfolio.create;
- begin
- inherited ;
- FStocks := TList.Create ;
- end;
-
- //----------------------------------------------------------
- destructor TPortfolio.destroy;
- var
- i : integer ;
- begin
- // Destroy all the TStockTrans objects owned by the
- // TList, FStocks
- for i := 0 to FStocks.Count - 1 do
- TObject( FStocks.Items[i] ).Free ;
- FStocks.Free ;
- inherited ;
- end;
-
- //----------------------------------------------------------
- procedure TPortfolio.LoadData;
- begin
- Stocks.Add( TStockTrans.CreateExt( 'BHP', 'Broken Hill Prop',
- 500, 17.50 )) ;
- Stocks.Add( TStockTrans.CreateExt( 'AMC', 'Amcor',
- 700, 7.20 )) ;
- Stocks.Add( TStockTrans.CreateExt( 'CML', 'Coles Myer',
- 500, 6.90 )) ;
- Stocks.Add( TStockTrans.CreateExt( 'NAB', 'National Bank',
- 200, 22.00 )) ;
- Stocks.Add( TStockTrans.CreateExt( 'TLS', 'Telstra',
- 400, 7.50 )) ;
- end;
-
- // Calculate the value of the portfolio (sum all the values
- // of the stocks in the portfolio)
- //----------------------------------------------------------
- function TPortfolio.Total: real;
- var
- i : integer ;
- begin
- result := 0 ;
- for i := 0 to Stocks.count - 1 do
- result := result + ( TObject( Stocks.Items[i] ) As
- TStockTrans ).Value ;
- end;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TStockTrans
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // An extended constructor (hence the name CreateExt) to
- // simplify the creation of TStockTrans instances
- constructor TStockTrans.createExt( const pStockCode : string ;
- const pStockName : string ;
- const pQty : integer ;
- const pPrice : real ) ;
- begin
- Create ;
- StockCode := pStockCode ;
- StockName := pStockName ;
- Qty := pQty ;
- Price := pPrice ;
- end ;
-
- // Calculate the value of a given stock holding
- //----------------------------------------------------------
- function TStockTrans.GetValue: real;
- begin
- result := Price * Qty ;
- end;
-
- initialization
- // Create an instance of the Subject (portfolio object)
- gPortfolio ;
-
- finalization
- uPortfolio.free;
-
- end.
-